import d4dset1
from datetime import date

FILE_ANT_ID_ABIDJAN = "ant_in_abidjan.txt"


def init_ant_ids_in_abidjan():
    ant_ids = []
    with open(FILE_ANT_ID_ABIDJAN, 'r') as finput:
        for line in finput:
            ant_id = line.strip()
            ant_ids.append(ant_id)
    return ant_ids

def count_ants_calls(ant_ids, start_date, end_date, outputfile=None):
    for ant_id in ant_ids:
        for cur_date in d4dset1.date_range(start_date, end_date):
            calls = d4dset1.sum_ant_related_all_calls(ant_id, cur_date)
            outputstr = "{}\t{}\t{}".format(ant_id, cur_date, calls)
            if (outputfile is None):  #write to file or screen
                print (outputstr)
            else:
                outputfile.write(outputstr+'\n')

            
if __name__ == '__main__':
    from time import clock
    start = clock() 
    start_date = date(2011,12,5)
    end_date = date(2012,4,22)
    with open('ant_in_abidjan_calls.tsv','w') as outputfile:
        count_ants_calls(init_ant_ids_in_abidjan(), start_date, end_date,
                outputfile)  #output result to file
    end = clock()
    print "total {} seconds".format(end-start)
